package org.jeecg.modules.wlhy.shiboetc;

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class Main {
    public static void main(String[] args) throws NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, NoSuchAlgorithmException {
        String plaintext = "Hello World"; // 要加密的明文

        byte[] keyBytes = new byte[24]; // 生成随机的24字节长度的密钥数组
        SecretKey secretKey = new SecretKeySpec(keyBytes, "DESede"); // 创建SecretKey对象

        Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); // 选择3DES算法及其模式为CBC、填充方式为PKCS5Padding

        String str="3213abcd";
        byte[] bytes = str.getBytes();

        IvParameterSpec ivParamSpec = new IvParameterSpec(bytes); // 自定义IV偏移量
//        IvParameterSpec ivParamSpec = new IvParameterSpec(new byte[]{0x12, 0x34, 0x56, 0x78, (byte) 0x9a, (byte) 0xbc, (byte) 0xde, (byte) 0xf0}); // 自定义IV偏移量
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParamSpec); // 初始化cipher并传入密钥和IV参数

        byte[] encryptedData = cipher.doFinal(plaintext.getBytes()); // 执行加密操作

        System.out.println("Encrypted Data: " + Base64.getEncoder().encodeToString(encryptedData)); // 输出加密后的结果
    }
}
